let app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startX: 0,
    startY: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var open=wx.getStorageSync('open');
    console.log(options);
     var match_id=options.match_id;
    if(open==undefined ){ 
     wx.navigateTo({
       url: '../index/login',
     })
    }
    if(open==""){ 
     wx.navigateTo({
       url: '../index/index',
     })
    }
    var that = this;
    let d=app.globalData;
            this.setData({
                open:open,
                platform:d.platform,    
                width:d.windowWidth,
                height:d.windowHeight,
                statusBarHeight:d.statusBarHeight,
                navHeight:d.navHeight,
                marginTop:d.navTitleTop,
                domain:d.domain,
                iUrl:d.iUrl,
                upUrl:d.upUrl,
                match_id:match_id
              })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getIndex();
  },
  goBack:function(){
    wx.navigateBack();
  },
  goto(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    var index=e.currentTarget.dataset.index;
    app.goto(url,index)

},
 getIndex(){
     let that=this;
     var open=wx.getStorageSync('open');
     console.log(open);
     wx.request({
        url: app.globalData.domain+'/wechat/user/index',
        data: { open_id:open.id},
        success: function (res){
            var data=res.data;
          console.log(data);
          that.setData({
              list:data.list
          })
        }
     })
 },
 touchStart(e) {
    console.log('touchStart=====>', e);
    let list = this.data.list;
    list.forEach(item => {
      if (item.isTouchMove) {
        item.isTouchMove = !item.isTouchMove;
      }
    });
    this.setData({
      list: list,
      startX: e.touches[0].clientX,
      startY: e.touches[0].clientY
    })
  },
  touchMove(e) {
    console.log('touchMove=====>', e);
    let moveX = e.changedTouches[0].clientX;
    let moveY = e.changedTouches[0].clientY;
    let indexs = e.currentTarget.dataset.index;
    let list = this.data.list;
console.log(moveX);
    let angle = this.angle({
      X: this.data.startX,
      Y: this.data.startY
    }, {
      X: moveX,
      Y: moveY
    });
   console.log(angle);
    list.forEach((item, index) => {
      item.isTouchMove = false;
      // 如果滑动的角度大于30° 则直接return；
      if (angle > 30) {
        return;
      }

      if (indexs === index) {
          console.log('indexs'+indexs)
          console.log('index'+index)
        if (moveX > this.data.startX) { // 右滑
          item.isTouchMove = false;
        } else { // 左滑
          item.isTouchMove = true;
        }
      }
    })

    this.setData({
      list
    })
  },

  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  delUser(e){
    console.log(e);
    var id=e.currentTarget.dataset.id;
    var open=wx.getStorageSync('open');
    let that=this;
    wx.showModal({
      title: '重要提醒',
      content: '您确定要删除该用户么？',
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
            wx.request({
                url: app.globalData.domain+'match/user/delUser',
                data: { open_id:open.id,id:id},
              success:res=>{
                    console.log(res);
                    that.getIndex();
              }
            })
        }
      }
    })
  },
  selectUser(e){
    console.log(e);
    var id=e.currentTarget.dataset.id;
    var open=wx.getStorageSync('open');
    var list=this.data.list;
    var user=list[id];
    console.log(user);
    console.log(list);
    var thispages = getCurrentPages(); //当前页面
        var navPage = thispages[thispages.length - 2]; //上一页
        navPage.setData({
            user:user,
            select:1
        })
        //navPage.getNumberType(user.sex);
        wx.navigateBack({
            delta: 1
        });

  },
  editUser(e){
    console.log(e);
    let d = e.currentTarget.dataset;
    var id=d.id;
    var match_id = d.match_id;
    wx.navigateTo({
      url: 'edit?id='+id+'&match_id='+match_id,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})